package com.cedifie.lendapp.dv_data

import android.content.Context
import com.cedifie.lendapp.config.CedConfigs
import com.cedifie.lendapp.http.ApiService
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.http.IHttpCallback
import com.cedifie.lendapp.utils.helper.SharedHelper
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.CompletableFuture

object CedDvManage {

    fun updateCedDvData(context: Context, status: String) {
        try {
            val exc = CedDvMode(context, CedConfigs.CED_APPNAME, SharedHelper.getCedToken())
            if (status.isBlank()) return

            if (status.contains("T2")) {
                exc.reportCedDvBty(ApiService.ANALYSIS_PATH.CED_ANALYSIS_BATTERY_PATH) { ba ->
                    val p = ApiService.getHttpUrl(ApiService.API_URL.CED_API_COMMON_URL)
                    val bty = listOf(
                        CompletableFuture.supplyAsync {
                            postCedAction(p, ba, "ba")
                        }
                    )

                    CompletableFuture.allOf(*bty.toTypedArray()).thenRun { }
                }
            }

            val stacks = mutableListOf<CompletableFuture<String>>()
            if (status.contains("T1")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        val p = ApiService.getHttpUrl(ApiService.API_URL.CED_API_COMMON_URL)
                        val dv = exc.reportCedDvDevice(ApiService.ANALYSIS_PATH.CED_ANALYSIS_DEVICE_PATH)
                        postCedAction(p, dv, "dv")
                    }
                )
            }

            if (status.contains("T3")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        val p = ApiService.getHttpUrl(ApiService.API_URL.CED_API_COMMON_URL)
                        val hd = exc.reportCedDvHdw(ApiService.ANALYSIS_PATH.CED_ANALYSIS_HARDWAVE_PATH)
                        postCedAction(p, hd, "hd")
                    }
                )
            }

            if (status.contains("T4")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        val p = ApiService.getHttpUrl(ApiService.API_URL.CED_API_COMMON_URL)
                        val rm = exc.reportCedDvStag(ApiService.ANALYSIS_PATH.CED_ANALYSIS_STORGE_PATH)
                        postCedAction(p, rm, "rm")
                    }
                )
            }

            if (status.contains("T5")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        val p = ApiService.getHttpUrl(ApiService.API_URL.CED_API_COMMON_URL)
                        val wf = exc.reportCedDvWf(ApiService.ANALYSIS_PATH.CED_ANALYSIS_WIFI_PATH)
                        postCedAction(p, wf, "wf")
                    }
                )
            }

            if (status.contains("T6")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        val p = ApiService.getHttpUrl(ApiService.API_URL.CED_API_COMMON_URL)
                        val ap = exc.reportCedDvApp(ApiService.ANALYSIS_PATH.CED_ANALYSIS_APPLIST_PATH)
                        postCedAction(p, ap, "ap")
                    }
                )
            }

            if (status.contains("T7")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        val p = ApiService.getHttpUrl(ApiService.API_URL.CED_API_COMMON_URL)
                        val mg = exc.reportCedDvMsage(ApiService.ANALYSIS_PATH.CED_ANALYSIS_SMS_PATH)
                        postCedAction(p, mg, "mg")
                    }
                )
            }

            if (stacks.isEmpty()) return

            CompletableFuture.allOf(*stacks.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    private fun postCedAction(toUrl: String, body: String?, tid: String): String {
        try {
            if (toUrl.isBlank() or body.isNullOrBlank()) return tid

            HttpHelper.postCedAction(toUrl, JSONObject(body),object : IHttpCallback{
                override fun onRequestSuccess(data: JSONObject) {
                }
                override fun onRequestError(error: JSONObject) {
                }
                override fun onRequestFinish() {
                }
            })
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return tid
    }
}